package com.centsuse.my_redis.controller;

import com.centsuse.my_redis.es.entity.KnowledgeDocument;
import com.centsuse.my_redis.service.impl.KnowledgeDocumentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author bobo
 */
@RestController
@RequestMapping("/api/knowledge")
public class KnowledgeDocumentController {

    @Autowired
    private KnowledgeDocumentServiceImpl knowledgeService;

    @PostMapping
    public ResponseEntity<KnowledgeDocument> create(@RequestBody KnowledgeDocument document) {
        KnowledgeDocument savedDoc = knowledgeService.save(document);
        return ResponseEntity.ok(savedDoc);
    }

    @PostMapping("/batch")
    public ResponseEntity<Iterable<KnowledgeDocument>> createBatch(@RequestBody List<KnowledgeDocument> documents) {
        Iterable<KnowledgeDocument> savedDocs = knowledgeService.saveAll(documents);
        return ResponseEntity.ok(savedDocs);
    }

    @GetMapping("/{id}")
    public ResponseEntity<KnowledgeDocument> getById(@PathVariable String id) {
        Optional<KnowledgeDocument> document = knowledgeService.findById(id);
        return document.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping
    public ResponseEntity<Page<KnowledgeDocument>> getAll(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<KnowledgeDocument> documents = knowledgeService.findAll(pageable);
        return ResponseEntity.ok(documents);
    }

    @PutMapping("/{id}")
    public ResponseEntity<KnowledgeDocument> update(@PathVariable String id, @RequestBody KnowledgeDocument documentDetails) {
        // 通常先检查是否存在，再设置ID进行全量更新
        if (!knowledgeService.existsById(id)) {
            return ResponseEntity.notFound().build();
        }
        documentDetails.setTaskId(id);
        KnowledgeDocument updatedDoc = knowledgeService.save(documentDetails);
        return ResponseEntity.ok(updatedDoc);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable String id) {
        if (!knowledgeService.existsById(id)) {
            return ResponseEntity.notFound().build();
        }
        knowledgeService.deleteById(id);
        return ResponseEntity.ok().build();
    }

    // --- 调用自定义查询的接口 ---

    @GetMapping("/task/{taskId}")
    public ResponseEntity<List<KnowledgeDocument>> getByTaskId(@PathVariable String taskId) {
        List<KnowledgeDocument> docs = knowledgeService.findByTaskId(taskId);
        return ResponseEntity.ok(docs);
    }

    @GetMapping("/file/{fileId}")
    public ResponseEntity<List<KnowledgeDocument>> getByFileId(@PathVariable String fileId) {
        List<KnowledgeDocument> docs = knowledgeService.findByFileId(fileId);
        return ResponseEntity.ok(docs);
    }

    @GetMapping("/para/{paraId}")
    public ResponseEntity<KnowledgeDocument> getByParaId(@PathVariable String paraId) {
        KnowledgeDocument doc = knowledgeService.findByParaId(paraId);
        if (doc != null) {
            return ResponseEntity.ok(doc);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/search/title")
    public ResponseEntity<List<KnowledgeDocument>> searchByTitle(@RequestParam String keyword) {
        List<KnowledgeDocument> docs = knowledgeService.findByKnTitleContaining(keyword);
        return ResponseEntity.ok(docs);
    }

    @GetMapping("/search/content")
    public ResponseEntity<List<KnowledgeDocument>> searchByContent(@RequestParam String keyword) {
        List<KnowledgeDocument> docs = knowledgeService.findByContentContaining(keyword);
        return ResponseEntity.ok(docs);
    }

    // ... 其他自定义查询的接口
}